using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using sqengine.Engine.Common;


namespace sqengine.Components
{

    public class SqMessage
    {
        public string text;
        public string texture_filename;
        public Texture2D texture;
        public Color color;
        public double millisecond = 500;
    }

    public class SqMessageQueue
    {
        public SqMessage message;
        public double _fadedelay = .025;
        public int _alphavalue = 255;

    }
    public class MessagesSystem : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private AdvancedContent _adcontent;
        private List<SqMessageQueue> _messages_queue = new List<SqMessageQueue>();
        private Game _game;
        private SpriteFont _font;

        private bool _started;


        private int mFadeIncrement = -1;
        private int _spacing = 20;

        private SpriteBatch _spritebatch;


        public MessagesSystem(Game game, AdvancedContent adcontent)
            : base(game)
        {
            _adcontent = adcontent;
            _game = game;

        }

        public override void Initialize()
        {
            _spritebatch = new SpriteBatch(_game.GraphicsDevice);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (_started)
            {
                for (int i = 0; i < _messages_queue.Count; i++)
                {
                    _messages_queue[i].message.millisecond -= gameTime.ElapsedGameTime.TotalMilliseconds;

                    if (_messages_queue[i].message.millisecond <= 0)
                    {
                        _messages_queue[i]._alphavalue += mFadeIncrement;

                        if (_messages_queue[i]._alphavalue == 0)
                        {
                            _messages_queue.Remove(_messages_queue[i]);
                        }
                    }
                }
            }
            base.Update(gameTime);

        }

        public override void Draw(GameTime gameTime)
        {
            if (_font == null)
                _font = _adcontent.LoadSpriteFont("Fonts\\Visitor");

            if (_started)
            {
                _spritebatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

                for (int i = 0; i < _messages_queue.Count; i++)
                {
                    byte R = _messages_queue[i].message.color.R;
                    byte G = _messages_queue[i].message.color.G;
                    byte B = _messages_queue[i].message.color.B;


                    if (_messages_queue[i].message.texture != null)
                    {
                        float msg_y = (_spacing / 2 + _messages_queue[i].message.texture.Height  * i);

                        _spritebatch.Draw(_adcontent.LoadTexture2D("Textures/UI/blank"), new Rectangle(0, (int)(msg_y - (_messages_queue[i].message.texture.Height / 2) + 10), (int)(_font.MeasureString(_messages_queue[i].message.text).X + _messages_queue[i].message.texture.Width) + 20, _messages_queue[i].message.texture.Width ), new Color(Color.Black.R, Color.Black.G, Color.Black.B, (byte)MathHelper.Clamp(100, 0, 255))); 
                        
                        _spritebatch.Draw(_messages_queue[i].message.texture, new Rectangle(0, (int)(msg_y - (_messages_queue[i].message.texture.Height / 2) + 10), _messages_queue[i].message.texture.Width, _messages_queue[i].message.texture.Height), new Color(Color.White.R, Color.White.G, Color.White.B, (byte)MathHelper.Clamp(_messages_queue[i]._alphavalue, 0, 255)));
                        //_spritebatch.DrawString(_font, _messages_queue[i].message.text, new Vector2(_messages_queue[i].message.texture.Width + 10, msg_y), new Color(Color.White.R, Color.White.G, Color.White.B, (byte)MathHelper.Clamp(_messages_queue[i]._alphavalue, 0, 255)));
                        //_spritebatch.DrawString(_font, _messages_queue[i].message.text, new Vector2(_messages_queue[i].message.texture.Width + 11, msg_y + 1), new Color(R, G, B, (byte)MathHelper.Clamp(_messages_queue[i]._alphavalue, 0, 255)));
                        Functions.DrawChat(_spritebatch, _font, new Vector2(_messages_queue[i].message.texture.Width + 11, msg_y + 1), _messages_queue[i].message.text);

                    }
                    else
                    {
                        int _space = 0;
                        if (i != 0)
                        {
                            if (_messages_queue[i - 1].message.texture != null)
                                _space = _spacing / 2 + _messages_queue[i -1].message.texture.Height * (i - 1);
                        }


                        //_spritebatch.Draw(_adcontent.LoadTexture2D("Textures/UI/blank"), new Rectangle(19, 0, 100, 100), new Color(Color.Gray.R,Color.Gray.G,Color.Gray.B,(byte)MathHelper.Clamp(100, 0, 255) ));
                        
                        _spritebatch.DrawString(_font, _messages_queue[i].message.text, new Vector2(20, _spacing * i + 20 + _space), new Color(Color.White.R, Color.White.G, Color.White.B, (byte)MathHelper.Clamp(_messages_queue[i]._alphavalue, 0, 255)));
                        _spritebatch.DrawString(_font, _messages_queue[i].message.text, new Vector2(21, _spacing * i + 21 + _space), new Color(R, G, B, (byte)MathHelper.Clamp(_messages_queue[i]._alphavalue, 0, 255)));

                    }

                }
                _spritebatch.End();
            }


            base.Draw(gameTime);
        }

        public void Start()
        {
            _started = true;
        }

        public void Stop()
        {

            _started = false;
        }

        public void AddItemToQueue(SqMessage message)
        {
            SqMessageQueue smq = new SqMessageQueue();
            smq.message = message;
            _messages_queue.Add(smq);
        }

        public void QuickAdd(string message, Color color, double delay, string texture)
        {
            SqMessage sqmess = new SqMessage();
            SqMessageQueue smq = new SqMessageQueue();

            sqmess.color = color;
            sqmess.millisecond = delay;
            sqmess.text = message;
            sqmess.texture_filename = texture;

            if (sqmess.texture_filename != "")
                sqmess.texture = _adcontent.LoadTexture2D(texture);

            smq.message = sqmess;
            _messages_queue.Add(smq);
        }


    }
}